﻿using Demo.UserService.Entities;
using Demo.UserService.Services;
using Demo.UserService.Services.Dtos;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Servicecomb.Saga.Omega.Abstractions.Transaction;
using System.Security.Claims;
using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc;

namespace Demo.UserService.Controllers
{

    /// <summary>
    /// 用户控制器（模拟微服务业务子系统）
    /// </summary>
    [ApiController]
    [Route("/api/user")]
    public class UserController : AbpController, IUserService
    {

        public IUserService _userService { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public UserController()
        {

        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<UserInfoDto> CreateAsync(UserInfoDto input)
        {
            try
            {

                return await _userService.CreateAsync(input);
            }
            catch (Exception e )
            {

                throw;
            }
        }

        /// <summary>
        /// 删除用户（增加权限校验）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpDelete] 
        public async Task DeleteAsync(Guid id)
        {
            // 1、获取用户信息
            ClaimsPrincipal claimsPrincipal = HttpContext.User;
            // 2、执行删除操作
            await _userService.DeleteAsync(id);
        }

        /// <summary>
        /// GetAsync
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("single")] 
        public async Task<UserInfoDto> GetAsync([FromQuery] Guid id)
        {
            return await _userService.GetAsync(id);
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("list")] 
        public async Task<PagedResultDto<UserInfoDto>> GetListAsync([FromQuery] PagedAndSortedResultRequestDto input)
        {
            return await _userService.GetListAsync(input);
        }

        /// <summary>
        /// 注册会员
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("addUser")]
        [Compensable(nameof(AddUserFailedHandle))]
        public async Task<UserInfoDto> AddUserAsync(UserInfoDto dto)
        {
            return await _userService.AddUserAsync(dto);
        }

        /// <summary>
        /// 失败触发
        /// </summary>
        /// <param name="dto"></param>
        void AddUserFailedHandle(UserInfoDto dto)
        {
            try
            {

                var jsonData = JsonConvert.SerializeObject(dto);
            }
            catch (Exception e)
            {

                throw;
            }
        } 

        /// <summary>
        /// Update
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<UserInfoDto> UpdateAsync(Guid id, UserInfoDto input)
        {
            return await _userService.UpdateAsync(id, input);
        }


    }
}
